/*
 * NextAction.java
 *
 * Created on June 22, 2005, 8:33 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */
package com.bff.mpd.musicjukebox.lib.actions;

import com.bff.mpd.utils.Utils;
import java.awt.event.ActionEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import org.bff.javampd.MPDAdmin;
import org.bff.javampd.MPDOutput;
import org.bff.javampd.exception.MPDConnectionException;
import org.bff.javampd.exception.MPDResponseException;

/**
 *
 * @author Bill
 */
public class OutputAction extends AbstractAction {

    private MPDAdmin admin;
    private MPDOutput output;

    /** Creates a new instance of StopAction */
    public OutputAction(MPDAdmin admin, MPDOutput output) {
        super(Utils.getMessage("OUTPUT_BUTTON_DISABLE"));
        this.admin = admin;
        this.output = output;

        if (output == null) {
            setEnabled(false);
        } else {
            checkOutput();
        }
    }

    @Override
    public void actionPerformed(ActionEvent evt) {
        try {
            if (this.output.isEnabled()) {
                getAdmin().disableOutput(output);
                putValue(NAME, Utils.getMessage("OUTPUT_BUTTON_ENABLE"));
            } else {
                getAdmin().enableOutput(output);
                putValue(NAME, Utils.getMessage("OUTPUT_BUTTON_DISABLE"));
            }
        } catch (MPDConnectionException ex) {
            Logger.getLogger(OutputAction.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MPDResponseException ex) {
            Logger.getLogger(OutputAction.class.getName()).log(Level.SEVERE, null, ex);
        }        
    }

    /**
     * @return the admin
     */
    public MPDAdmin getAdmin() {
        return admin;
    }

    /**
     * @param admin the admin to set
     */
    public void setAdmin(MPDAdmin admin) {
        this.admin = admin;
    }

    /**
     * @return the output
     */
    public MPDOutput getOutput() {
        return output;
    }

    /**
     * @param output the output to set
     */
    public void setOutput(MPDOutput output) {
        if (output == null) {
            setEnabled(false);
        } else {
            setEnabled(true);
            this.output = output;
            checkOutput();
        }
    }

    private void checkOutput() {
        if (this.output.isEnabled()) {
            putValue(NAME, Utils.getMessage("OUTPUT_BUTTON_DISABLE"));
        } else {
            putValue(NAME, Utils.getMessage("OUTPUT_BUTTON_ENABLE"));
        }
    }
}
